package com.summer;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.UUID;

@Controller
public class fcontroller {
//更换用户头像
    @RequestMapping("/upload")
    @ResponseBody
    public String upload (@RequestParam("file") MultipartFile file) {
        // 获取原始名字
        String fileName = file.getOriginalFilename();
        // 获取后缀名
        // String suffixName = fileName.substring(fileName.lastIndexOf("."));
        // 文件保存路径
        String filePath = "D:/桌面/夏日速递2.0/summer2.0/summer/src/main/resources/";
//        G:\summer\src\main\resources\templates
        // 文件重命名，防止重复
        String name="/static/images/"+UUID.randomUUID()+ fileName;
        fileName = filePath + name;
        System.out.println("路径：   "+name);
        // 文件对象
        File dest = new File(fileName);
        // 判断路径是否存在，如果不存在则创建
        if(!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            // 保存到服务器中
            file.transferTo(dest);
            return name;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "上传失败";
    }


    @RequestMapping("/as")
    public String ssss()
    {
        return "up";
    }


    @RequestMapping("download")
    public void download(HttpServletResponse response) throws Exception {
        // 文件地址，真实环境是存放在数据库中的
        File file = new File("D:\\upload\\a.txt");
        // 穿件输入对象
        FileInputStream fis = new FileInputStream(file);
        // 设置相关格式
        response.setContentType("application/force-download");
        // 设置下载后的文件名以及header
        response.addHeader("Content-disposition", "attachment;fileName=" + "a.txt");
        // 创建输出对象
        OutputStream os = response.getOutputStream();
        // 常规操作
        byte[] buf = new byte[1024];
        int len = 0;
        while((len = fis.read(buf)) != -1) {
            os.write(buf, 0, len);
        }
        fis.close();
    }


    @RequestMapping("/shz")
    public String na()
    {
      return "up";
    }


    @RequestMapping("/testfile")
    @ResponseBody
    public String test( MultipartFile file)
    {
        System.out.println(file);
        return "ojk";
    }

    @RequestMapping("/pupload")
    @ResponseBody
    public String pupload (@RequestParam("file") MultipartFile file) {
        System.out.println("文件"+file);
        String fileName = file.getOriginalFilename();
//        String filePath = "G:/summer/src/main/resources/static/images/product/";
        String filePath = "D:/桌面/MVC专综项目/summer6.3/summer/src/main/resources/static/images/product/";
//        D:\桌面\MVC专综项目\summer6.3\summer\src\main\resources\templates\images,项目根目录
        // 文件重命名，防止重复
        String sname=UUID.randomUUID()+ fileName;
        String name="static/images/product/"+sname;
        fileName = filePath + sname;
        System.out.println("路径：   "+name);
        // 文件对象
        File dest = new File(fileName);
        // 判断路径是否存在，如果不存在则创建
        if(!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            // 保存到服务器中
            file.transferTo(dest);

            return name;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "上传失败";
    }

    @RequestMapping("/ssa")
    public String ssa()
    {
        return "shoppCar2";
    }
    @RequestMapping("/ssaa")
    public String ssaa()
    {
        return "user";
    }
}
